<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>找出最大值</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.log.html">log</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.min.html">min</a></div>
 <div class="up"><a href="ref.math.html">Math 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.max" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">max</h1>
  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">max</span> &mdash; <span class="dc-title">找出最大值</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.max-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>max</strong></span>
    ( <span class="methodparam"><span class="type">array</span> <code class="parameter">$values</code></span>
   )</div>

  <div class="methodsynopsis dc-description">
   <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>max</strong></span>
    ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$value1</code></span>
   , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$value2</code></span>
   [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$...</code></span>
  ] )</div>

  <p class="para rdfs-comment">
   如果仅有一个参数且为数组，<span class="function"><strong>max()</strong></span>
   返回该数组中最大的值。如果第一个参数是整数、字符串或浮点数，则至少需要两个参数而
   <span class="function"><strong>max()</strong></span> 会返回这些值中最大的一个。可以比较无限多个值。
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    PHP 会将非数值的 <span class="type"><a href="language.types.string.html" class="type string">string</a></span> 当成
    <em>0</em>，但如果这个正是最大的数值则仍然会返回一个字符串。如果多个参数都求值为
    <em>0</em> 且是最大值，<span class="function"><strong>max()</strong></span> 会返回其中数值的
    <em>0</em>，如果参数中没有数值的 <em>0</em>，则返回按字母表顺序最大的字符串。
   </p>
  </p></blockquote>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.max-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">values</code></em></dt>

     <dd>

      <p class="para">
       包含了多个值的数组。
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">value1</code></em></dt>

     <dd>

      <p class="para">
       Any <a href="language.operators.comparison.html" class="link">comparable</a>
       value.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">value2</code></em></dt>

     <dd>

      <p class="para">
       Any <a href="language.operators.comparison.html" class="link">comparable</a>
       value.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">...</code></em></dt>

     <dd>

      <p class="para">
       Any <a href="language.operators.comparison.html" class="link">comparable</a>
       value.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.max-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   <span class="function"><strong>max()</strong></span> 返回参数中数值最大的值。
   If multiple values can be considered of the same size,
   the one that is listed first will be returned.
  </p>
  <p class="para">
   When <span class="function"><strong>max()</strong></span> is given multiple <span class="type"><a href="language.types.array.html" class="type array">array</a></span>s, the
   longest array is returned. If all the arrays have the same length,
   <span class="function"><strong>max()</strong></span> will use lexicographic ordering to find the return
   value.
  </p>
  <p class="para">
   When given a <span class="type"><a href="language.types.string.html" class="type string">string</a></span> it will be cast as an <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>
   when comparing.
  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.max-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-3372">
    <p><strong>Example #1 使用 <span class="function"><strong>max()</strong></span> 的例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">max</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">6</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">7</span><span style="color: #007700">);&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;7<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">max</span><span style="color: #007700">(array(</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">));&nbsp;</span><span style="color: #FF8000">//&nbsp;5<br /><br />//&nbsp;When&nbsp;'hello'&nbsp;is&nbsp;cast&nbsp;as&nbsp;integer&nbsp;it&nbsp;will&nbsp;be&nbsp;0.&nbsp;Both&nbsp;the&nbsp;parameters&nbsp;are&nbsp;equally<br />//&nbsp;long,&nbsp;so&nbsp;the&nbsp;order&nbsp;they&nbsp;are&nbsp;given&nbsp;in&nbsp;determines&nbsp;the&nbsp;result<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">max</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'hello'</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;0<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">max</span><span style="color: #007700">(</span><span style="color: #DD0000">'hello'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;hello<br /><br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">max</span><span style="color: #007700">(</span><span style="color: #DD0000">'42'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;'42'<br /><br />//&nbsp;Here&nbsp;0&nbsp;&gt;&nbsp;-1,&nbsp;so&nbsp;'hello'&nbsp;is&nbsp;the&nbsp;return&nbsp;value.<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">max</span><span style="color: #007700">(-</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'hello'</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;hello<br /><br />//&nbsp;With&nbsp;multiple&nbsp;arrays&nbsp;of&nbsp;different&nbsp;lengths,&nbsp;max&nbsp;returns&nbsp;the&nbsp;longest<br /></span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">max</span><span style="color: #007700">(array(</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">),&nbsp;array(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">));&nbsp;</span><span style="color: #FF8000">//&nbsp;array(1,&nbsp;1,&nbsp;1,&nbsp;1)<br /><br />//&nbsp;对多个数组，max&nbsp;从左向右比较。<br />&nbsp;&nbsp;&nbsp;//&nbsp;因此在本例中：2&nbsp;==&nbsp;2，但&nbsp;4&nbsp;&lt;&nbsp;5<br /></span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">max</span><span style="color: #007700">(array(</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">8</span><span style="color: #007700">),&nbsp;array(</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">7</span><span style="color: #007700">));&nbsp;</span><span style="color: #FF8000">//&nbsp;array(2,&nbsp;5,&nbsp;7)<br /><br />//&nbsp;如果同时给出数组和非数组作为参数，则总是将数组视为<br />&nbsp;&nbsp;&nbsp;//&nbsp;最大值返回<br /></span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">max</span><span style="color: #007700">(</span><span style="color: #DD0000">'string'</span><span style="color: #007700">,&nbsp;array(</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">7</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">42</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;array(2,&nbsp;5,&nbsp;7)<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    </div> 
  </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.max-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.min.html" class="function" rel="rdfs-seeAlso">min()</a> - 找出最小值</span></li>
    <li class="member"><span class="function"><a href="function.count.html" class="function" rel="rdfs-seeAlso">count()</a> - 计算数组中的单元数目或对象中的属性个数</span></li>
   </ul>
  </p>
 </div>

</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.log.html">log</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.min.html">min</a></div>
 <div class="up"><a href="ref.math.html">Math 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
